﻿/* EntitiesToDTOs. Copyright (c) 2011. Fabian Fernandez.
 * http://entitiestodtos.codeplex.com
 * Licensed by Common Development and Distribution License (CDDL).
 * http://entitiestodtos.codeplex.com/license
 * Fabian Fernandez. 
 * http://www.linkedin.com/in/fabianfernandezb/en
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntitiesToDTOs.Properties;
using System.Xml.Linq;

namespace EntitiesToDTOs.Helpers
{
    /// <summary>
    /// Helps in the tasks relative to DTOs.
    /// </summary>
    internal class EntityDTOHelper
    {
        /// <summary>
        /// Gets a cleaned name of an Entity.
        /// </summary>
        /// <param name="entityName">Entity name.</param>
        /// <returns></returns>
        public static string GetPureNameFromEntityName(string entityName)
        {
            // Validate Entity name
            if (string.IsNullOrWhiteSpace(entityName))
            {
                throw new ArgumentException(Resources.Error_InvalidEntityName);
            }

            // Backup Entity name
            string backupEntityName = (string.Empty + entityName);

            // Check for Entity and EntitySet words
            if (entityName.Contains(Resources.NameEntity))
            {
                entityName = entityName.Remove(
                    entityName.IndexOf(Resources.NameEntity), Resources.NameEntity.Length);
            }
            else if (entityName.Contains(Resources.NameEntitySet))
            {
                entityName = entityName.Remove(
                    entityName.IndexOf(Resources.NameEntitySet), Resources.NameEntitySet.Length);
            }

            // Check if we got an empty Entity name, in that case use the backup Entity name
            if (string.IsNullOrWhiteSpace(entityName))
            {
                entityName = backupEntityName;
            }

            return entityName;
        }

        /// <summary>
        /// Gets the DTO Name from an Entity Name.
        /// </summary>
        /// <param name="entityName"></param>
        /// <returns></returns>
        public static string GetDTOName(string entityName)
        {
            entityName = EntityDTOHelper.GetPureNameFromEntityName(entityName);

            return (entityName + Resources.NameTailDTO);
        }

        /// <summary>
        /// Gets the Class Base Type of an EntityType node.
        /// </summary>
        /// <param name="entityNode">EntityType node to get the Class Base Type.</param>
        /// <returns></returns>
        public static string GetEntityBaseType(XElement entityNode)
        {
            string entityBaseType = null;

            if (entityNode.Attribute(EdmxNodeAttributes.EntityType_BaseType) != null)
            {
                entityBaseType = entityNode.Attribute(EdmxNodeAttributes.EntityType_BaseType).Value;

                string[] entityBaseTypeSplitted =
                    entityBaseType.Split(new string[] { Resources.Dot }, StringSplitOptions.RemoveEmptyEntries);

                entityBaseType = entityBaseTypeSplitted[(entityBaseTypeSplitted.Length - 1)];
            }

            return entityBaseType;
        }

        /// <summary>
        /// Gets the DTO Class Base Type of an EntityType node.
        /// </summary>
        /// <param name="entityNode">EntityType node to get the DTO Class Base Type.</param>
        /// <returns></returns>
        public static string GetEntityDTOBaseType(XElement entityNode)
        {
            string entityBaseType = EntityDTOHelper.GetEntityBaseType(entityNode);

            if (entityBaseType == null)
            {
                return null;
            }

            return EntityDTOHelper.GetDTOName(entityBaseType);
        }
    }
}